/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.utn.tpsimulacion02;

import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.xy.IntervalXYDataset;


/**
 *
 * @author aaltamir
 */
public class ChartManager {
           
    private static IntervalXYDataset generarDatos(int cant_Intervalos, List<Double> numeros)
    {
        HistogramDataset dataset = new HistogramDataset();
                
        double num [] = new double [numeros.size()];
        
        for(int i=0; i < num.length; i++)
        {
            num[i] = numeros.get(i);
        }
              
        dataset.addSeries("Distribucion de frecuencias", num, cant_Intervalos);
        
        return dataset;
     }
    
    public static void generarChart(int cant_Intervalos, List<Double> numeros)
    {
        IntervalXYDataset dataset = generarDatos(cant_Intervalos, numeros);
        JFreeChart chart = ChartFactory.createHistogram("Histograma",null,null,dataset,PlotOrientation.VERTICAL,true,true,false);
        XYPlot plot = (XYPlot) chart.getPlot();
        XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        
        ChartFrame frame = new ChartFrame("Grafico de Barras",chart);
        frame.pack();
        frame.setExtendedState(ChartFrame.MAXIMIZED_BOTH);
        frame.setVisible(true);
   }
}

        /*
        double mayor = ChartManager.getMayor(collection);
        double menor = ChartManager.getMenor(collection);
        Intervalo intervalo[];
        double lim_Inferior;
        double lim_Superior;
        double amplitud;
        
        
        Si el mayor es 0.0 entonces todos los nuemros de la coleccion son negativos,
        
        
        if (mayor == 0.0)
        {
            amplitud = Math.abs(menor /cant_Intervalos);
        }
        
        else
        {
            amplitud = mayor /cant_Intervalos;   
        }
        
        intervalo = new Intervalo[cant_Intervalos];
        lim_Inferior=menor;
        lim_Superior=menor + amplitud;
        
        int i;
        for (i=0; i<cant_Intervalos; i++)
            {
                intervalo[i]= new Intervalo(lim_Inferior,lim_Superior);
                lim_Inferior= lim_Superior;
                lim_Superior= lim_Superior+amplitud;
            }
        for (int j=0; j < cant_Intervalos; j++){
                
                for (double num: collection){
                    
                    if(num >=intervalo[j].getLimInferior() && num <intervalo[j].getLimSuperior()){
                        intervalo[j].agregarNumero(num);
                    }
                }
            }
        
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        CategoryAxis categoryAxis = new CategoryAxis("Valores");
        ValueAxis valueAxis = new NumberAxis("Numeros");
        valueAxis.setNegativeArrowVisible(true);
        valueAxis.setPositiveArrowVisible(true);
        for ( i=0; i < intervalo.length; i++){
            
            dataset.addValue(intervalo[i].getContador(), "Frecuencia Observada" ,"[" + intervalo[i].getLimInferior() + " - " + intervalo[i].getLimSuperior()+"]");
            
        }
        
        LineAndShapeRenderer renderer = new LineAndShapeRenderer();
        
        //BarRenderer renderer = new BarRenderer();
        
        CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis,
        renderer);
        //JFreeChart chart= ChartFactory.createBarChart3D("Distribucion de Frecuencias", "Intervalos", "Frecuencia", dataset, PlotOrientation.VERTICAL,false,true,false);
        
        JFreeChart chart = new JFreeChart("Distribucion de Frecuencias", JFreeChart.DEFAULT_TITLE_FONT,plot, false);
        ChartFrame frame = new ChartFrame("Grafico de barras", chart);
        //frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setExtendedState(ChartFrame.MAXIMIZED_BOTH);
        frame.setVisible(true);    
    }
    
    private static double getMayor(Collection<Double> coleccion){
        Iterator it = coleccion.iterator();
        double mayor = 0;
        double x;
        
        while (it.hasNext())
        {
            x = (Double)it.next();
            
            if (x> mayor)
                mayor = x;               
        }
        return mayor;
     }
    
    private static double getMenor(Collection<Double> coleccion){
        
        Iterator it = coleccion.iterator();
        double menor = 0.0;
        double x;
        
        while (it.hasNext())
        {
            x = (Double)it.next();
            
            if (x < menor)
                menor = x;                     
        }
        return menor;
     }
     * 
     */
